package syyrjx.jdbc;


import syyrjx.jdbc.util.JDBCTool;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

public class DataBase {

    public static Boolean register(int id,String username,String userpwd,String sex){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCTool.getConnection();
            String sql = "insert into userinfo values (?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.setString(2,username);
            ps.setString(3,userpwd);
            ps.setString(4,"0".equals(sex)?"保密":("1".equals(sex)?"男":"女"));
            int count = ps.executeUpdate();
            if(count>0){
                JDBCTool.count++;
                return true;
            }
            return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCTool.JDBCClose(null,ps,conn);
        }
        return false;
    }

    public static boolean logIn(String username,String userpwd){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = JDBCTool.getConnection();
            String sql = "select no,username,sex from userinfo where username=? and userpwd=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,userpwd);
            rs = ps.executeQuery();
            return rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            JDBCTool.JDBCClose(rs,ps,conn);
        }
        return false;
    }
    public static Map<String,Object> show(int id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Map<String,Object> uInfo = new HashMap<>();
        try {
            conn = JDBCTool.getConnection();
            String sql = "select no,username,userpwd,sex from userinfo where no = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                uInfo.put("id",rs.getInt("no"));
                uInfo.put("username",rs.getString("username"));
                uInfo.put("userpwd",rs.getString("userpwd"));
                uInfo.put("sex",rs.getString("sex"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCTool.JDBCClose(rs,ps,conn);
        }
        return uInfo;
    }

    public static Map<Integer,Map<String,Object>> show() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Map<Integer,Map<String,Object>> uInfo = new HashMap<>();
        for(int i = 0;i<JDBCTool.count;i++){
            uInfo.put(i,show(i));
        }
        return uInfo;
    }

    public static boolean delete(String id){
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        int no = Integer.valueOf(id);
        try {
            conn = JDBCTool.getConnection();
            String sql = "delete from userinfo where no=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,no);
            count = ps.executeUpdate();
            if(no<JDBCTool.count&&count>0){
                for(int i = no+1;i <JDBCTool.count;i++){
                    String modifyNo = "update userinfo set no=? where no=?";
                    ps = conn.prepareStatement(modifyNo);
                    ps.setInt(1,i-1);
                    ps.setInt(2,i);
                    int g =ps.executeUpdate();
                    System.out.println(g);
                }
            }
            JDBCTool.count--;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCTool.JDBCClose(null,ps,conn);
        }
        return count>0;
    }

//    public static void main(String[] args) {
//        System.out.println(delete("0"));
//    }
}
